﻿Imports System.Drawing.Drawing2D


Public Class Canvas
	Inherits UserControl

	'	Shadows Autosize As Boolean = True

	'	Dim Buttons As New List(Of Button)
	'	Dim WithEvents Timer As New Timer With {.Interval = 10}
	'	Dim EmptyCursor As New Cursor((New Bitmap(1, 1)).GetHicon)
	'	Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Integer) As Integer

	'	Dim m_FocusedButton As Button
	'	Property FocusedButton As Button
	'		Get
	'			Return m_FocusedButton
	'		End Get
	'		Set(ByVal Value As Button)
	'			If m_FocusedButton Is Value Then Exit Property
	'			If Not m_FocusedButton Is Nothing Then Invalidate(m_FocusedButton.Rect)
	'			m_FocusedButton = Value
	'			If Not m_FocusedButton Is Nothing Then Invalidate(m_FocusedButton.Rect)
	'		End Set
	'	End Property

	Sub New()
		InitializeComponent()
		DoubleBuffered = True
		ResizeRedraw = True
		'		Buttons.Add(New Button("", 0, Me))
		'		Buttons.Add(New Button("closepic", 1, Me))
		'		Buttons.Add(New Button("full", 1, Me))
		'		Buttons.Add(New Button("zoom", 1, Me))
		'		Buttons.Add(New Button("first", 1, Me))
		'		Buttons.Add(New Button("prev", 1, Me))
		'		Buttons.Add(New Button("slideshow", 1, Me))
		'		Buttons.Add(New Button("next", 1, Me))
		'		Buttons.Add(New Button("last", 1, Me))
		'		Buttons.Add(New Button("rotate1", 1, Me))
		'		Buttons.Add(New Button("rotate3", 1, Me))
		'		Buttons.Add(New Button("prev2", 2, Me))
		'		Buttons.Add(New Button("next2", 2, Me))
	End Sub

	'	Dim FadeOut As Boolean = False
	'	Dim m_Level As Double
	'	Property Level As Double
	'		Get
	'			Return m_Level
	'		End Get
	'		Set(ByVal Value As Double)
	'			If Value < 0 Then Value = 0 : Timer.Enabled = False
	'			If Value > 12 Then Value = 12 : FadeOut = FocusedButton Is Nothing
	'			If m_Level = Value Then Exit Property
	'			If Not (m_Level >= 1 And Value >= 1) Then
	'				For Each Button In Buttons
	'					Invalidate(Button.Rect)
	'				Next
	'			End If
	'			m_Level = Value
	'		End Set
	'	End Property

	'	Dim m_Image As Photo
	'	Property Photo As Photo
	'		Get
	'			Return m_Image
	'		End Get
	'		Set(ByVal Value As Photo)
	'			m_Image = Value
	'			Autosize = True
	'			FixRect()
	'			'Invalidate()
	'		End Set
	'	End Property

	'	Dim Rect As Rectangle

	'	Sub FixRect()
	'		Dim R As Rectangle = Me.ClientRectangle
	'		If Rect.Width < R.Width Then
	'			Rect.Location = New Point((R.Width - Rect.Width) / 2, Rect.Top)
	'		Else
	'			If Rect.Left > 0 Then Rect.Offset(-Rect.Left, 0)
	'			If Rect.Right < R.Right Then Rect.Offset(R.Right - Rect.Right, 0)
	'		End If
	'		If Rect.Height < R.Height Then
	'			Rect.Location = New Point(Rect.Left, (R.Height - Rect.Height) / 2)
	'		Else
	'			If Rect.Top > 0 Then Rect.Offset(0, -Rect.Top)
	'			If Rect.Bottom < R.Bottom Then Rect.Offset(0, R.Bottom - Rect.Bottom)
	'		End If
	'		Invalidate()
	'	End Sub

	'	Dim Point As Point

	'	Event ButtonClicked(ByVal Key As String)

	'	Private Sub Canvas_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
	'		If Not FocusedButton Is Nothing Then
	'			Dim Key As String = FocusedButton.Name
	'			If Key.EndsWith("2") Then Key = Key.Substring(0, Key.Length - 1)
	'			RaiseEvent ButtonClicked(Key)
	'		End If
	'	End Sub

	'	Private Sub Canvas_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
	'		Point = e.Location
	'	End Sub

	'	Private Sub Canvas_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave, Me.LostFocus
	'		FocusedButton = Nothing
	'	End Sub

	'	Private Sub Canvas_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
	'		If e.Button = Windows.Forms.MouseButtons.Left Then
	'			If Not Autosize Then
	'				Rect.Offset(e.X - Point.X, e.Y - Point.Y)
	'				FixRect()
	'				Point = e.Location
	'			End If
	'		Else
	'			FadeOut = False
	'			Timer.Enabled = True
	'			For Each Button In From b In Buttons Where b.Type <> 0
	'				If Button.Rect.Contains(e.Location) Then
	'					FocusedButton = Button
	'					Exit Sub
	'				End If
	'			Next
	'			FocusedButton = Nothing
	'		End If
	'	End Sub

	'	Private Sub Canvas_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
	'		Dim r As Double = e.Delta / 2000
	'		Autosize = False
	'		Dim px = (e.X - Rect.Left) / Rect.Width : Dim w = Rect.Width
	'		Dim py = (e.Y - Rect.Top) / Rect.Height : Dim h = Rect.Height
	'		Rect.Inflate(Rect.Width * r, 0)
	'		If Rect.Width < 120 Then Rect.Width = 120
	'		If Rect.Width > Photo.Width * 16 Then Rect.Inflate(-(Rect.Width - Photo.Width * 16) / 2, 0)
	'		Rect.Inflate(0, -(Rect.Height - Rect.Width * Photo.AR) / 2)
	'		Rect.Offset(-(px - 0.5) * (Rect.Width - w), -(py - 0.5) * (Rect.Height - h))
	'		FixRect()
	'	End Sub

	'	Sub ToggleFullsize()
	'		If Rect.Width <> Photo.Width Then
	'			Autosize = False
	'			Rect.Inflate((Photo.Width - Rect.Width) \ 2, (Photo.Height - Rect.Height) \ 2)
	'			Rect.Size = New Size(Photo.Width, Photo.Height)
	'		Else
	'			Autosize = True
	'		End If
	'		FixRect()
	'	End Sub

	Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
		MyBase.OnPaint(e)
		If File = "" Then Return
		Images.DrawImage(e.Graphics, ClientRectangle, File, Height)
		'e.Graphics.DrawImage(Studio.getth
	End Sub

	'	Private Sub Picture_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
	'		If Photo Is Nothing Then Exit Sub
	'		Dim watch As New Stopwatch
	'		watch.Start()
	'		Dim c As Rectangle = Me.ClientRectangle
	'		Dim G As Graphics = e.Graphics
	'		If Autosize OrElse (Rect.Width < c.Width And Rect.Height < c.Height) Then
	'			Rect = c
	'			Dim z As Integer = Rect.Width * Photo.AR
	'			If z < Rect.Height Then
	'				Rect.Inflate(0, -(Rect.Height - z) / 2)
	'			Else
	'				z = Rect.Height / Photo.AR
	'				Rect.Inflate(-(Rect.Width - z) / 2, 0)
	'			End If
	'		End If
	'		If Level < 1 Then
	'			Cursor = EmptyCursor
	'		ElseIf Not FocusedButton Is Nothing Then
	'			Cursor = Cursors.Hand
	'		Else
	'			If Autosize Then Cursor = Cursors.Default Else Cursor = Cursors.SizeAll
	'		End If
	'		Dim Image As Bitmap = Photo.BestImage(Rect.Height)
	'		Dim k As New Rectangle(-Rect.Left / Rect.Width * Image.Width, -Rect.Top / Rect.Height * Image.Height, c.Width / Rect.Width * Image.Width, c.Height / Rect.Height * Image.Height)
	'		G.PixelOffsetMode = PixelOffsetMode.HighQuality
	'		G.InterpolationMode = InterpolationMode.HighQualityBicubic
	'		G.DrawImage(Image, c, k, GraphicsUnit.Pixel)
	'		'Debug.Print(k.Width & "-" & k.Height & " (" & watch.ElapsedMilliseconds & ")")
	'		e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
	'		e.Graphics.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
	'		For Each Button In Buttons
	'			Button.Paint(e.Graphics, If(Me.Level > 1, 1, Me.Level), Button Is FocusedButton)
	'		Next
	'	End Sub

	'	Private Sub Canvas_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
	'		FixRect()
	'		Dim s As Integer = 48
	'		Dim sep As Integer = 12
	'		Dim m As Integer = 2
	'		Buttons(0).Rect = New Rectangle(0, Height - s - m * 2, Width, s + m * 2)
	'		Buttons(1).Rect = New Rectangle(Width - s - 1, 0, s, s)
	'		Dim TotalWidth As Integer = 9 * s + 2 * sep
	'		Dim R As New Rectangle(Width \ 2 - TotalWidth \ 2, Height - s - 2, s, s)
	'		Buttons(2).Rect = R : R.Offset(s, 0)
	'		Buttons(3).Rect = R : R.Offset(s + sep, 0)
	'		Buttons(4).Rect = R : R.Offset(s, 0)
	'		Buttons(5).Rect = R : R.Offset(s, 0)
	'		Buttons(6).Rect = R : R.Offset(s, 0)
	'		Buttons(7).Rect = R : R.Offset(s, 0)
	'		Buttons(8).Rect = R : R.Offset(s + sep, 0)
	'		Buttons(9).Rect = R : R.Offset(s, 0)
	'		Buttons(10).Rect = R : R.Offset(s, 0)
	'		Buttons(11).Rect = New Rectangle(0, Height / 2 - s, s * 2, s * 2)
	'		Buttons(12).Rect = New Rectangle(Right - s * 2, Height / 2 - s, s * 2, s * 2)
	'	End Sub

	'	Class Button
	'		Public Rect As New Rectangle(0, 0, 0, 0)
	'		Public Name As String
	'		Public Type As Integer
	'		Public Parent As Canvas

	'		Sub New(ByVal Name As String, ByVal Type As Integer, ByVal Parent As Canvas)
	'			Me.Name = Name
	'			Me.Type = Type
	'			Me.Parent = Parent
	'		End Sub




	'		Sub Paint(ByVal G As Graphics, ByVal l As Double, ByVal Focused As Boolean)
	'			Dim R As Rectangle = Rect
	'			If Type = 0 Then
	'				G.FillRectangle(Brushes.White.A(0.25 * l), R)
	'			ElseIf Type = 1 Then
	'				R.Inflate(-4, -4)
	'				G.FillRoundRect(R, Brushes.Black.A(0.5 * l * If(Focused, 1, 0.4)), 12)
	'			End If
	'			R.Inflate(-8, -8)
	'			Dim Path As New GraphicsPath(FillMode.Winding)
	'			Dim Fill As Boolean = False
	'			Select Case Name
	'				Case "closepic"
	'					R.Inflate(-2, -2)
	'					Path.StartFigure() : Path.AddLine(R.Left, R.Top, R.Right, R.Bottom)
	'					Path.StartFigure() : Path.AddLine(R.Right, R.Top, R.Left, R.Bottom)
	'				Case "full"
	'					Dim m As Integer = 4
	'					Path.StartFigure() : Path.AddLines(R.Left + m, R.Top, R.Left, R.Top, R.Left, R.Top + m) ' : Path.CloseFigure()
	'					Path.StartFigure() : Path.AddLines(R.Left + m, R.Bottom, R.Left, R.Bottom, R.Left, R.Bottom - m) ' : Path.CloseFigure()
	'					Path.StartFigure() : Path.AddLines(R.Right - m, R.Top, R.Right, R.Top, R.Right, R.Top + m) ': Path.CloseFigure()
	'					Path.StartFigure() : Path.AddLines(R.Right - m, R.Bottom, R.Right, R.Bottom, R.Right, R.Bottom - m) ' : Path.CloseFigure()
	'					R.Inflate(-m - 2, -m - 2)
	'					Path.AddRectangle(R)
	'				Case "rotate1"
	'					R.Inflate(-2, -2)
	'					Path.StartFigure() : Path.AddArc(R, 45, 225)
	'					Path.StartFigure() : Path.AddLines(R.Left + R.Width \ 2 - 5, R.Top - 1, R.Left + R.Width \ 2, R.Top, R.Left + R.Width \ 2 - 4, R.Top + 4)
	'					Path.StartFigure()
	'				Case "rotate3"
	'					R.Inflate(-2, -2)
	'					Path.StartFigure() : Path.AddArc(R, 135, -225)
	'					Path.StartFigure() : Path.AddLines(R.Left + R.Width \ 2 + 5, R.Top - 1, R.Left + R.Width \ 2, R.Top, R.Left + R.Width \ 2 + 4, R.Top + 4)
	'					Path.StartFigure()
	'				Case "last"
	'					R.Inflate(-5, -2)
	'					R.Offset(-R.Width \ 2, 0)
	'					Path.StartFigure() : Path.AddLines(R.Left, R.Top, R.Right, R.Top + R.Height \ 2, R.Left, R.Bottom) : Path.CloseFigure()
	'					R.Offset(R.Width, 0)
	'					R.Inflate(-2, -2)
	'					Path.StartFigure() : Path.AddLines(R.Left, R.Top, R.Right, R.Top + R.Height \ 2, R.Left, R.Bottom) : Path.CloseFigure()
	'					Fill = True
	'				Case "next"
	'					R.Inflate(-5, -2)
	'					Path.StartFigure() : Path.AddLines(R.Left, R.Top, R.Right, R.Top + R.Height \ 2, R.Left, R.Bottom) : Path.CloseFigure()
	'					Fill = True
	'				Case "next2"
	'					'Dim pp = (From v In CType(Parent.FindForm(), FrmMain).List.Items Where TypeOf v Is Photo And v.Index > Studio.Current.Index)
	'					'If pp.Any Then Dim Photo As Photo = pp.First : DrawImageWithOpacity(G, Photo.BestImage(R.Width), Photo.FixAR(R), 0.5 * l)
	'					'R.Inflate(-20, -20)
	'					'R.Inflate(-5, -2)
	'					'Path.StartFigure() : Path.AddLines(R.Left, R.Top, R.Right, R.Top + R.Height \ 2, R.Left, R.Bottom) : Path.CloseFigure()
	'					'Fill = True
	'				Case "prev"
	'					R.Inflate(-5, -2)
	'					Path.StartFigure() : Path.AddLines(R.Right, R.Top, R.Left, R.Top + R.Height \ 2, R.Right, R.Bottom) : Path.CloseFigure()
	'					Fill = True
	'				Case "prev2"
	'					R.Inflate(-20, -20)
	'					R.Inflate(-5, -2)
	'					Path.StartFigure() : Path.AddLines(R.Right, R.Top, R.Left, R.Top + R.Height \ 2, R.Right, R.Bottom) : Path.CloseFigure()
	'					Fill = True
	'				Case "first"
	'					R.Inflate(-5, -2)
	'					R.Offset(R.Width \ 2, 0)
	'					Path.StartFigure() : Path.AddLines(R.Right, R.Top, R.Left, R.Top + R.Height \ 2, R.Right, R.Bottom) : Path.CloseFigure()
	'					R.Offset(-R.Width, 0)
	'					R.Inflate(-2, -2)
	'					Path.StartFigure() : Path.AddLines(R.Right, R.Top, R.Left, R.Top + R.Height \ 2, R.Right, R.Bottom) : Path.CloseFigure()
	'					Fill = True
	'			End Select
	'			Dim Pen As Pen = Pens.White.A(l * If(Focused, 1, 0.4))
	'			Pen.Width = 4
	'			Pen.StartCap = LineCap.Round
	'			Pen.EndCap = LineCap.Round
	'			Pen.LineJoin = LineJoin.Round
	'			If Fill Then
	'				G.FillPath(New SolidBrush(Pen.Color), Path)
	'			Else
	'				G.DrawPath(Pen, Path)
	'			End If
	'		End Sub
	'	End Class

	'	Private Sub Timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer.Tick
	'		Level += If(FadeOut, -0.1, 0.3)
	'	End Sub

	'	Event Maximize()

	'	Private Sub Canvas_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DoubleClick
	'		If FocusedButton Is Nothing Then RaiseEvent Maximize()
	'	End Sub

	Dim _File As String
	Property File As String
		Get
			Return _File
		End Get
		Set(ByVal Value As String)
			_File = Value
			Invalidate()
		End Set
	End Property
End Class
